 /**
 * 逻辑、功能相关描述: <br>
 *
 * @author 付湘辉  <br>
 * 编写日期：2008-8-6  <br>
 */
package com.ce.pms.charge.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;

import com.ce.pms.base.dao.impl.PmsBaseDaoImpl;
import com.ce.pms.charge.dao.IPrePaySchDao;
import com.ce.pms.charge.model.PrePaySchDomain;
import com.ce.pms.charge.model.SchDetail;
import com.ce.pms.charge.vo.PrePaySchSearchVO;
import com.ce.pms.charge.vo.PrePaySchVO;
import com.ce.pms.common.model.SysInfo;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.common.util.StringAnalysiUtil;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.support.Page;

/**
 * 	名称: PrePaySchDaoImpl <br>
 *        预交方案管理Dao
 *
 * @author  付湘辉  <br>
 * Created on： 2008-8-6 <br>
 * @author <br>
 * update on： <br>
 * why: <br>
 */
public class PrePaySchDaoImpl extends PmsBaseDaoImpl<PrePaySchDomain> implements IPrePaySchDao {

	/**
	 * 功能描述:获取所有的方案明细，key 方案ID，value 方案明细LIST<br>
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-9-27<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public Map<Long,List<SchDetail>> queryAllSchDetail(){
		String hql="FROM SchDetail sd ORDER BY sd.prePaySch,sd.seqence ASC";
		Query query=createQuery(hql, null);
		List<SchDetail> schDetailList= query.list();
		Map<Long,List<SchDetail>> result=new HashMap<Long,List<SchDetail>>();
		List<SchDetail> list=new ArrayList<SchDetail>();

		Long tempId=null;
		for(SchDetail item : schDetailList){
			if(null!=tempId && item.getPrePaySch().getPrePaySchId()!=tempId){
				list=new ArrayList<SchDetail>();
			}

			list.add(item);
			result.put(item.getPrePaySch().getPrePaySchId(), list);
			tempId=item.getPrePaySch().getPrePaySchId();
		}
		return result;
	}

	/**
	 * 功能描述: 得到默认的预交方案DO实体 <br>
	 * @return PrePaySchDomain
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-7  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public PrePaySchDomain getDefaultPrePaySch(){
		String queryStr = "FROM PrePaySchDomain pps WHERE pps.isDefault=1 ";
		Query query =super.createQuery(queryStr);
		List list = query.list();
		if (SysrefcodeDO.CONSTANTS_ZERO < list.size()) {
			return (PrePaySchDomain)list.get(0);
		}

		return null;
	}

	/**
	 * 功能描述: 根据预交方案名称查询一个预交方案DO信息 <br>
	 * @param schName
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-6  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public PrePaySchDomain getPrePaySchByName(String schName){
		if (BlankUtil.isBlank(schName)) {
			return null;
		}
		return this.getPrePaySchBy(null, schName);
	}

	/**
	 * 功能描述: 根据预交方案id和名称查询一个预交方案DO信息 <br>
	 * @param schName
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-6  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public PrePaySchDomain getPrePaySchByNoSetIdAndName(Long prePaySchId, String schName){
		if (BlankUtil.isBlank(prePaySchId) || BlankUtil.isBlank(schName)) {
			return null;
		}
		return this.getPrePaySchBy(prePaySchId,schName);
	}

	/**
	 * 功能描述: 通过预交款子账户id查询预交方案实体 <br>
	 * @param prePayAccountId
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-6  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public PrePaySchDomain findPrePaySchByPrePayAccountId(Long prePayAccountId){

		String queryStr = "SELECT pps FROM PrePaySchDomain pps INNER JOIN pps.prePayAccountes acs WHERE acs.id=? ";
		Query query =super.createQuery(queryStr,prePayAccountId);
		List list = query.list();
		if (SysrefcodeDO.CONSTANTS_ZERO < list.size()) {
			return (PrePaySchDomain)list.get(0);
		}
		return null;
	}

	/**
	 * 功能描述: 通过id得到一个预交方案详细DO实体 <br>
	 * @param schDetailId
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-7  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public SchDetail getSchDetailById(Long schDetailId){
		String queryStr = "FROM SchDetail sd WHERE sd.id=? ";
		Query query =super.createQuery(queryStr, schDetailId);
		Object obj = query.uniqueResult();
		if (BlankUtil.isBlank(obj)) {
			return null;
		}
		return (SchDetail)obj;
	}

	/**
	 * 功能描述: 查询预交方案分页列表信息 <br>
	 * @param preSearch
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-6  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public Page queryPagedPrePaySchs(PrePaySchSearchVO preSearch){

		StringBuffer sb = new StringBuffer("FROM PrePaySchDomain PPS ");
		String schemeName = preSearch.getSchemeName();
		if (!BlankUtil.isBlank(schemeName) && !BlankUtil.isBlank(schemeName.trim())) {
			schemeName=StringAnalysiUtil.toTropicalString(schemeName.trim(), "'_%");
			sb.append("WHERE PPS.schemeName LIKE '%").append(schemeName).append("%' ");
		}
		sb.append("ORDER BY ");
		if (!BlankUtil.isBlank(preSearch.getSortStr())) {
			String [] sortStr = preSearch.getSortStr().split(SysInfo.SEPARATOR_COMMA,2);
			sb.append("PPS.");
			for (String string : sortStr) {
				sb.append(string+" ");
			}
		}else{
			//默认以名称排序
			sb.append("PPS.schemeName ");
		}

		Page page = super.pagedQueryByPageNo(sb.toString(), preSearch.getPageNo(), preSearch.getPageSize());
		return page;
	}

	/**
	 * 功能描述: 查询一个预交方案DO信息 <br>
	 * @param schName
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-6  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private PrePaySchDomain getPrePaySchBy(Long prePaySchId, String schName){
		StringBuffer queryStr = new StringBuffer("FROM PrePaySchDomain PPS ");
		if (!BlankUtil.isBlank(prePaySchId)) {
			queryStr.append("WHERE PPS.id<>") .append(prePaySchId).append(" AND PPS.schemeName=? ");
		}else{
			queryStr.append("WHERE PPS.schemeName=? ");
		}

		Query query =super.createQuery(queryStr.toString(), schName);
		List list = query.list();
		if (SysrefcodeDO.CONSTANTS_ZERO < list.size()) {
			return (PrePaySchDomain)list.get(0);
		}
		return null;
	}
}
